package com.summit.exam.servlet;


import com.summit.exam.dbdao.PaperDao;
import com.summit.exam.dbdao.QuestionDao;
import com.summit.exam.dbdao.TimerDao;
import com.summit.exam.entity.PaperBean;
import com.summit.exam.entity.QuestionBean;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

public class TestServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        String paperId = req.getParameter("paperId");

        //查看试题
        QuestionDao questionService = new QuestionDao();
        ArrayList<QuestionBean> questionArrayList = questionService.selectQuestion(Integer.parseInt(paperId));

        for (int i = 0; i < questionArrayList.size(); i++) {
            questionArrayList.get(i).setIndex(i + 1);
        }

        //查看试题名称
        PaperDao paperService = new PaperDao();
        PaperBean paper = paperService.oneselectInfo(paperId);

        req.setAttribute("title", paper.getTitle());
        req.getSession().setAttribute("title", paper.getTitle());
        req.getSession().setAttribute("paperId", paperId);
        req.getSession().setMaxInactiveInterval(6000);

        TimerDao timerService = new TimerDao();

        req.setAttribute("time", timerService.selectTime().getTime());
        req.setAttribute("questionArrayList", questionArrayList);

        req.getRequestDispatcher( "/ExamSystem/techweb/TestPaper.jsp").forward(req, resp);
    }
}
